package com.tevintao.marketingsystem.task;

import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.tevintao.marketingsystem.log.LogManager;
import com.tevintao.marketingsystem.services.ViewOperation;
import com.tevintao.marketingsystem.xposted.WxTaskManger;


public class ChangeSignatureTaskProcess extends SetTaskExtra {
    private static ChangeSignatureTaskProcess ourInstance = new ChangeSignatureTaskProcess();

    public static ChangeSignatureTaskProcess getInstance() {
        return ourInstance;
    }

    private ChangeSignatureTaskProcess() {
    }

    @SuppressWarnings("ResourceType")
    @Override
    public boolean processView(String contextName, View v) {
        if (WxUiViewConfig.MAIN_LAUNCH.equals(contextName)) {
            boolean inContact = WxUiViewConfig.inTab(v, WxUiViewConfig.TAB_ME);
            if (inContact) {
//                WxUiViewConfig.printViewGroup((ViewGroup) v);
                LogManager.getInstance().logInfo("当前位于我按钮所在的列表");
                final View myName = v.findViewById(WxUiViewConfig.MY_NAME_ITEM);
                LogManager.getInstance().logError("myName = " + myName);

                if (null != myName) {
                    ViewOperation.clickView(myName);
                }
            }
        } else if (WxUiViewConfig.VIEW_ME_INFO.equals(contextName)) {
            View signView = WxUiViewConfig.getViewByIdAndContent((ViewGroup) v, 16908310, "个性签名");
            LogManager.getInstance().logInfo("signView = " + signView);
            if (null != signView) {
                ViewOperation.clickView(signView);
            }
        } else if (WxUiViewConfig.VIEW_EDIT_SIGNATURE.equals(contextName)) {
            WxUiViewConfig.printViewGroup((ViewGroup) v);

            Object signObj = getExtra(WxUiViewConfig.KEY_MY_SIGNATURE);
            if (null == signObj) {
                taskError(v.getContext(), WxTaskManger.TASK_SATE_EXTRA_ERROR, "没有下发的签名内容");
                return true;
            }

            View inputSign = v.findViewById(WxUiViewConfig.INPUT_SIGN_EDIT);
            if (inputSign instanceof EditText) {
                TextView singTextView = (TextView) inputSign;
                String signContext = singTextView.getText().toString();
                if (!signContext.equals(signObj.toString())) {
                    ViewOperation.setText(singTextView, signObj.toString());
                } else {
                    View saveSign = v.findViewById(WxUiViewConfig.SAVE_SIGN_BUTTON);
                    if (null != saveSign) {
                        ViewOperation.clickView(saveSign);
                        taskError(v.getContext(), WxTaskManger.TASK_SATE_SUCCESS, "保存签名成功 " + signObj.toString());
                    }
                }
            }
        }

        return false;
    }
}
